package com.chain.qs.modules.biz.web;

import com.chain.qs.common.config.Global;
import com.chain.qs.common.utils.StringUtils;
import com.chain.qs.common.web.BaseController;
import com.chain.qs.modules.biz.entity.EquipmentGroup;
import com.chain.qs.modules.biz.entity.StationGroup;
import com.chain.qs.modules.biz.service.EquipmentGroupService;
import com.chain.qs.modules.biz.service.StationGroupService;
import com.chain.qs.modules.biz.service.StationService;
import com.chain.qs.modules.sys.entity.Office;
import com.google.common.collect.Maps;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 设备组Controller
 * @author Wang fgx
 */
@Controller
@RequestMapping(value = "${adminPath}/biz/sgroup")
public class StationGroupController extends BaseController {

    @Resource
    private EquipmentGroupService equipmentGroupService;
    @Resource
    private StationGroupService sgroupService;
    @Resource
    private EquipmentGroupService egService;
    @Resource
    private StationService stationService;

    @ModelAttribute("stationGroup")
    public StationGroup get(@RequestParam(required=false) String id,@RequestParam(value = "station.id", required = false)String stationId) {
        StationGroup stationGroup;
        if (StringUtils.isNotBlank(id)){
            stationGroup = sgroupService.get(id);
            stationGroup.setGroup(egService.get(stationGroup.getGroup().getId()));
        }else{
            stationGroup = new StationGroup();
        }
        if(stationId!=null) {
            stationGroup.setStation(stationService.get(stationId));
        }
        return  stationGroup;
    }


    @RequestMapping(value = {"sgroup"})
    public String sgroup(Model model) {
        model.addAttribute("list", stationService.findListWithOffice(new Office()));
        return "modules/biz/sGroupList";
    }

    @RequestMapping(value = {"list",""})
    public String list(StationGroup stationGroup, Model model) {
        List<StationGroup> slist=sgroupService.findList(stationGroup);
        model.addAttribute("list",slist);
        stationGroup.setStation(stationService.get(stationGroup.getStation().getId()));
        model.addAttribute("sgroup",stationGroup);
        return "modules/biz/stationGroupList";
    }

    @RequestMapping(value="form")
    public String form(StationGroup stationGroup, Model model){
//        List<EquipmentGroup> eglist = egService.findList(new EquipmentGroup());
//        model.addAttribute("eglist", null);
        Map<String,String> groupTypeMap = Maps.newHashMap();
//        int baseGroupCount = sgroupService.isBaseGroupExist(stationGroup.getStation().getId());
//        model.addAttribute("baseGroupCount",baseGroupCount);
        groupTypeMap.put("2","系统组");
        groupTypeMap.put("1","基础组");
        model.addAttribute("groupTypeMap",groupTypeMap.entrySet());
        model.addAttribute("sgroup",stationGroup);
        int gryType = 1;//默认基础组
        List<EquipmentGroup> egList;
        if(stationGroup.getGrpType()!=null) {
            gryType = Integer.parseInt(stationGroup.getGrpType());
        }
        egList = equipmentGroupService.findListByType(gryType);
        model.addAttribute("egList",egList);
        return "modules/biz/stationGroupForm";
    }

   @ResponseBody
    @RequestMapping(value = "save")
    public String save(StationGroup stationGroup){
        sgroupService.save(stationGroup);
        return "success";
    }



    @RequestMapping(value = "delete")
    public String delete(StationGroup stationGroup, RedirectAttributes redirectAttributes) {
        if(Global.isDemoMode()){
            addMessage(redirectAttributes, "演示模式，不允许操作！");
            return "redirect:" + adminPath + "/biz/equgroup";
        }
        sgroupService.delete(stationGroup);
        addMessage(redirectAttributes, "删除设备组成功");
        return "redirect:" + adminPath + "/biz/sgroup?station.id="+stationGroup.getStation().getId();
    }


    @RequestMapping(value = "sgroupSelect", method = RequestMethod.POST)
    public String sgroupSelect(HttpServletResponse response, String officeId) {
        return renderString(response, sgroupService.findListByOfficeId(officeId));
    }
}
